<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="95px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
              <el-form-item label="统编代码:">
                <el-input
                  clearable
                  v-model="form.zxspbm"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="12">
              <el-form-item label="产品名称:">
                <el-input
                  clearable
                  v-model="form.cpm"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="small"
                @click="reload">查询
              </el-button>
              <el-button
                type="primary"
                icon="el-icon-edit"
                class="ele-btn-icon"
                size="small"
                @click="editRate">修改
              </el-button>
              <el-button @click="reset" size="small">重置</el-button>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="15">
            <el-col :lg="4" :md="12">
              <el-form-item label="采购规格:">
                <el-input
                  clearable
                  disabled
                  v-model="form.cfgg"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="12">
              <el-form-item label="包装单位:">
                <el-input
                  disabled
                  v-model="form.bzdwmc"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="12">
              <el-form-item label="最小使用:">
                <el-input
                  disabled
                  v-model="form.yydwmc"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="12">
              <el-form-item label="零售包装:">
                <el-input
                  disabled
                  v-model="form.bznhsl"
                  placeholder=""/>
              </el-form-item>
            </el-col>
        </el-row>
        <el-row>
          <el-col :lg="4" :md="12">
              <el-form-item label="NC计量单位:">
                <el-input
                  disabled
                  v-model="form.zmeasname"
                  placeholder=""/>
              </el-form-item>
            </el-col>
          <el-col :lg="4" :md="12">
              <el-form-item label="转换规则:">
                <el-select
                v-model="form.converratetype"
                placeholder=""
                class="ele-fluid">
                <el-option label="乘" value="乘"/>
                <el-option label="除" value="除"/>
              </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="12">
              <el-form-item label="转换率:">
                <el-input
                  clearable
                  ref="txtConverrate"
                  v-model="form.converrate"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="12">
              <el-form-item label="">
                <el-checkbox v-model="setFlag">未设置</el-checkbox>
              </el-form-item>
            </el-col>
        </el-row>
        <el-row>
          <el-col :lg="8" :md="12">
              <el-form-item label="NC物料编码:">
                <el-input style="padding-top: 5px;"
                  disabled
                  v-model="form.invcode"
                  placeholder="">
                  <template slot="append">
                    <el-button
                    type="primary"
                    icon="el-icon-search"
                    class="ele-btn-icon"
                    size="small"
                    @click="openSelect">选择
                  </el-button>
                  </template>
                  </el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :md="12">
              <el-form-item label="NC物料名称:">
                <el-input
                  disabled
                  v-model="form.invname"
                  placeholder=""/>
              </el-form-item>
            </el-col>
        </el-row>

      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        :pageSize="100"
        @row-click="handleRowClick"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar" v-if="false">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:c37infypyhjl:add')">添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:c37infypyhjl:dall')">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:c37infypyhjl:edit')">修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此养护记录表吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:c37infypyhjl:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <InvDocList
      :dataItem="current"
      :visible.sync="showEdit"
      @done="setInvcode"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import moment from 'moment';
import InvDocList from './frmInvDocList.vue';
//import InvDocList from './frmInv56DocList.vue';

export default {
  name: 'ExamC37InfYpyhjl',
  components: {InvDocList},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/ygorder/converrate',
      // 表格列配置
      columns: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',
        //   fixed: "left"
        // },
        // {
        //   prop: 'riqiDate',
        //   label: '日期',
        //   sortable: 'custom',
        //   showOverflowTooltip: true,
        //   minWidth: 160,
        //   align: 'center',
        //   formatter: (row, column, cellValue) => {
        //     return this.dateFormat(row,column,cellValue);
        //   }
        // },
        {
          prop: 'zxspbm',
          label: '统编代码',
          showOverflowTooltip: true,
          minWidth: 180,
          align: 'center'
        },
        {
          prop: 'cpm',
          label: '产品名',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'scqymc',
          label: '生产企业',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'cfgg',
          label: '成分规格',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'bzdwmc',
          label: '包装单位',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'yydwmc',
          label: '最小使用',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'bznhsl',
          label: '零售包装',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'defname',
          label: '药品类型',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'converratetype',
          label: '转换规则',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'converrate',
          label: '转换率',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'invcode',
          label: '药品编码',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        {
          prop: 'invname',
          label: '药品名称',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'ts',
          label: 'ts',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'pkConverrate',
          label: 'pkConverrate',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'invalidflag',
          label: '是否作废',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'invalidmemo',
          label: '作废说明',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
      ],
      // 表格搜索条件
      where: {},
      form: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      setFlag:false,
    };
  },
  methods: {
    dateFormat(row, column,cellValue) {
        const daterc = row[column.property]
        if(daterc !=null){
            return moment(cellValue).format("YYYY-MM-DD")
            }
        },
        statusFormat(row, column,cellValue) {
        const daterc = row[column.property]
        if(daterc !=null){
            return cellValue.trim()=="N"?"已同步":"未同步";
            }
      },
      openSelect(){
        this.showEdit = true;
      },
      setInvcode(invcode,invname)
      {
        this.form.invcode = invcode;
        this.form.invname = invname;
        this.$refs.txtConverrate.focus();
      },
      editRate()
      {
        if(!this.form.invcode)
        {
          this.$message.info("请先根据药品规格选择药品编码");
          return;
        }
        if(!this.form.converrate)
        {
          this.$message.info("请输入转换率,转换率不能为空。");
          return;
        }
        if(isNaN(this.form.converrate))
        {
          this.$message.info("请输入有效的转化率");
          return;
        }
        this.$confirm('确定更新当前产品信息，将产品【' + this.form.cpm + '】的规格单位转换率设置为【' + this.form.converrate + '】？', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/ygorder/updaterate', this.form).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reset();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
      },
      handleRowClick(row)
      {
        //Object.assign(this.where,row);
        if(this.selection.length > 0){
        let del_row = this.selection.shift()
        this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
        }
        this.selection.push(row);
        this.current = row;
        this.$set(this.form,"zxspbm",row.zxspbm);
        this.$set(this.form,"cpm",row.cpm);
        this.$set(this.form,"cfgg",row.cfgg);
        this.$set(this.form,"bzdwmc",row.bzdwmc);
        this.$set(this.form,"yydwmc",row.yydwmc);
        this.$set(this.form,"bznhsl",row.bznhsl);
        this.$set(this.form,"zmeasname",row.zmeasname);
        this.$set(this.form,"converrate",row.bznhsl);
        // this.$set(this.form,"converrate",row.converrate);
        this.$set(this.form,"pkConverrate",row.pkConverrate);
        this.$set(this.form,"converratetype",row.converratetype);
        this.$set(this.form,"invcode",row.invcode);
        this.$set(this.form,"invname",row.invname);
        var rate = row.cfgg.replace('1g*','').replace('g/袋','');
        if(!this.form.converrate)
        {
          this.$set(this.form,"converrate",rate);

        }

      },
    /* 刷新表格 */
    reload() {
      this.where.zxspbm = this.form.zxspbm;
      this.where.cpm = this.form.cpm;
      this.where.nosetflag = this.setFlag
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.form = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/c37infypyhjl/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的养护记录表吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/c37infypyhjl/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
</style>
